//
//  MenuLayer.m
//  xGame
//
//  Created by Max on 11-8-14.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MenuLayer.h"
#import "GameLayer.h"
#import "ControlLayer.h"
#import "AppDelegate.h"

@implementation MenuLayer

+(CCScene *)scene
{
    CCScene *scene = [CCScene node];
    
    MenuLayer *layer =[MenuLayer node];
    
    [scene addChild:layer];
    
    return scene;
}

-(id)init
{
    if((self=[super init]))
    {
        [CCMenuItemFont setFontSize:25];
        
        [CCMenuItemFont setFontSize:30];
        
        CCLabelBMFont *newGameLabel= [CCLabelBMFont labelWithString:@"New Game" fntFile:@"font09.fnt"];
        CCMenuItemLabel *newGameItem= [CCMenuItemLabel itemWithLabel:newGameLabel target:self selector:@selector(newGame:)];
        newGameItem.scale = 0.8f;
        
        CCLabelBMFont *loadGameLabel =[CCLabelBMFont labelWithString:@"Hight Scores" fntFile:@"font09.fnt"];
        CCMenuItemLabel *loadGameItem =[CCMenuItemLabel itemWithLabel:loadGameLabel target:self selector:nil];
        loadGameItem.scale = 0.8f;
        
        CCLabelBMFont *settingLabel =[CCLabelBMFont labelWithString:@"Settings" fntFile:@"font09.fnt"];
        CCMenuItemLabel *settingItem =[CCMenuItemLabel itemWithLabel:settingLabel target:self selector:nil];
        settingItem.scale= 0.8f;
        
        CCLabelBMFont *helpLabel =[CCLabelBMFont labelWithString:@"Help" fntFile:@"font09.fnt"];
        CCMenuItemLabel *helpItem =[CCMenuItemLabel itemWithLabel:helpLabel target:self selector:nil];
        settingItem.scale= 0.8f;
        
        CCMenuItemFont *quitGame =[CCMenuItemFont itemFromString:@"Quit" target:self selector:@selector(onQuit:)];
        
        id colorAction =[CCTintBy actionWithDuration:0.5f red:0 green:-255 blue:-255];
        id colorBack = [colorAction reverse];
        id seq =[CCSequence actions:colorAction,colorBack, nil];
        [quitGame runAction:[CCRepeatForever actionWithAction:seq]];
        
        CCMenu *menu =[CCMenu menuWithItems:newGameItem,loadGameItem,settingItem,helpItem,quitGame, nil];
        [menu alignItemsVertically];
        
        //elastic effect
        CGSize size = [[CCDirector sharedDirector] winSize];
        int i=0;
        for (CCNode *child in [menu children]) {
            CGPoint destPoint = child.position;
            int offset = size.width/2 +50;
            if (i%2) {
                offset = -offset;
            }
            
            child.position = ccp(destPoint.x+offset, destPoint.y);
            [child runAction:[CCEaseElasticOut actionWithAction:[CCMoveBy actionWithDuration:2 position:ccp(destPoint.x-offset,0)] period:0.35f]];
            i++;
        }
        
        [self addChild:menu];
    }
    
    return self;
}

- (void) newGame:(id) sender
{
    CCTransitionScene *ts =[CCTransitionScene transitionWithDuration:0.5f scene:[GameLayer scene]];
	[[CCDirector sharedDirector] replaceScene:ts];
}

- (void) OnSettings:(id) sender
{
//	CCScene *sc = [CCScene node];
//	[sc addChild:[SettingsLayer node]];
//	
//	[[CCDirector sharedDirector] replaceScene: [CCShrinkGrowTransition transitionWithDuration:1.2f scene:sc]];
	
}

-(void) onQuit: (id) sender
{
    /*
     [[CCDirector sharedDirector] end];
     
     // HA HA... no more terminate on sdk v3.0
     // http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html
     if( [[UIApplication sharedApplication] respondsToSelector:@selector(terminate)] )
     [[UIApplication sharedApplication] performSelector:@selector(terminate)];
     else
     CCLOG(@"YOU CAN'T TERMINATE YOUR APPLICATION PROGRAMATICALLY in SDK 3.0+");
     */
}

-(void)dealloc
{
    [super dealloc];
}

@end
